package today;

import java.io.*;

public class ObjectStreamTest {
    public static void main(String[] args) {
        test01();
        test02();
        test03();
    }
    private  static  void test03(){
        System.out.println("==========");
        Student student=new Student("李四", "150001", 90);

        String fileName="D:\\temp/write.txt";
        writeObject(student,fileName);
        readObject(fileName);

        Object object=readObject(fileName);
        System.out.println("stu0 == objReadin:" + (student== object));

    }
    private static Object readObject(String fileName){
        //创建一个对象输入流实例变量
        Object obj = null;
        try {
            // 创建一个对象输入流实例变量
            ObjectInputStream ois = new ObjectInputStream(
                    new FileInputStream(
                            new File(fileName)));
            // 读取对象输出流文件
            obj = ois.readObject();
            // 输出读入的对象内容
            System.out.println("对象输入流读取对象成功：" + obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;

    }
    private static void writeObject(Object object, String fileName){
        try {
            ObjectOutputStream oos=new ObjectOutputStream(
                    new FileOutputStream(
                            new File(fileName)
                    )
            );
            oos.writeObject(object);//
            if (oos!=null){
                oos.flush();
                oos.close();
            }
            System.out.println("对象保存成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Object test02(){
        //创建一个对象输入流实例变量
        Object obj = null;
        try {
            // 创建一个对象输入流实例变量
            ObjectInputStream ois = new ObjectInputStream(
                    new FileInputStream(
                            new File("d:\\temp/file.txt")));
            // 读取对象输出流文件
            obj = ois.readObject();
            // 输出读入的对象内容
            System.out.println("对象输入流读取对象成功：" + obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;

    }
        //

    public static void test01(){
        //创建一个student的对象
        Student student=new Student("张三", "150001", 90);
        //创建一个对象输出流的实例对象
        try {
            ObjectOutputStream oos=new ObjectOutputStream(
                    new FileOutputStream(
                            new File("d:\\temp/file.txt")
                    )
            );
            oos.writeObject(student);//
            if (oos!=null){
                oos.flush();
                oos.close();
            }
            System.out.println("对象保存成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Student implements Comparable<Student>,Serializable{
   private  String name;
   private  String num;
   private  double score;
    @Override
    public String toString() {
        return "Student{" +
                "stuName='" + name + '\'' +
                ", stuNum='" + num + '\'' +
                ", stuScore=" + score +
                '}';
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public double getScore() {
        return score;
    }

    public void setScror(double scror) {
        this.score = scror;
    }

    public Student(String name, String num, double scror) {
        this.name = name;
        this.num = num;
        this.score= scror;
    }

    @Override
    public int compareTo(Student o) {
        return 0;
    }
}